package com.cxs.migrationdemo.slice;

import com.cxs.migrationdemo.ResourceTable;
import com.cxs.migrationdemo.util.DeviceUtils;
import com.cxs.migrationdemo.util.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuation {

    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");

    Button btnRunRemote, btnMigrationToRemote, btnMigrationBack;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 启动远程设备的FA
        btnRunRemote = (Button)findComponentById(ResourceTable.Id_btn_run_remote);  // 启动远程设备的FA
        btnRunRemote.setClickedListener(c -> {
            /***** 实现跨设备打开FA *****/
            // 获取远程设备的目标Id
            String deviceId = DeviceUtils.getDeviceId();

            Intent intent1 = new Intent();
            Operation build = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName("com.cxs.migrationdemo")
                    .withAbilityName("com.cxs.migrationdemo.RemoteAbility")
                    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)    // 表示该应用支持分布式调度，否则无法获得分布式能力
                    .build();
            intent1.setOperation(build);
            startAbility(intent1);

        });

        // 迁移到远程设备
        btnMigrationToRemote = (Button)findComponentById(ResourceTable.Id_btn_migration_to_remote);  // 迁移到远程设备
        btnMigrationToRemote.setClickedListener(c -> {
            Intent intent2 = new Intent();
            /*
                跳转到指定FA
                ElementName(String deviceId, String bundleName, String abilityName)
                deviceId：设备Id， 为空时代表本机
                bundleName: 包名
                abilityName: 要跳转的目标ability
             */
            ElementName migrationAbility =
                    new ElementName("", "com.cxs.migrationdemo", "MigrationAbility");
            intent2.setElement(migrationAbility);
            startAbility(intent2);
        });

        // 可迁回的迁移远程设备
        btnMigrationBack = (Button)findComponentById(ResourceTable.Id_btn_migration_back);  //可迁回的迁移远程设备
        btnMigrationBack.setClickedListener(c -> {
            Intent intent3 = new Intent();

            ElementName migrationBackAbility =
                    new ElementName("", "com.cxs.migrationdemo", "MigrationBackAbility");
            intent3.setElement(migrationBackAbility);
            startAbility(intent3);
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
